﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace CrankShaftAnalysisSystem
{
    class IK220
    {
        //声明初始化函数IK220Init
        [DllImport("ik220dll.dll", EntryPoint = "IK220Init", CallingConvention = CallingConvention.StdCall)]
        public static extern bool IK220Init(ushort ax);

        //Start counter with next reference mark
        [DllImport("ik220dll.dll", EntryPoint = "IK220StartRef", CallingConvention = CallingConvention.StdCall)]
        public static extern bool IK220StartRef(ushort ax);

        //声明计数器清零函数IK220Reset
        [DllImport("ik220dll.dll", EntryPoint = "IK220Reset", CallingConvention = CallingConvention.StdCall)]
        public static extern bool IK220Reset(ushort ax);

        //声明读取脉冲函数IK220Read48
        [DllImport("ik220dll.dll", EntryPoint = "IK220Read48", CallingConvention = CallingConvention.StdCall)]
        public static extern bool IK220Read48(ushort ax, ushort la, out double pData);

        //声明重置EnDat编码器至默认状态函数IK220ResetEn
        [DllImport("ik220dll.dll", EntryPoint = "IK220ResetEn", CallingConvention = CallingConvention.StdCall)]
        public static extern bool IK220ResetEn(ushort ax, out ushort pStatus);

        /// <summary>
        /// Axis: Number of the axis (0 to 15)
        /// </summary>
        /// <param name="ax"></param>
        /// <param name="pStatus">
        /// Pointer to a variable in which the EnDat status is
        /// saved.
        /// Low byte:
        /// 0 = OK
        /// 1 = Encoder does not answer or no encoder
        /// connected
        /// 2 = Transmission error
        /// 3 = Error mode echo
        /// 4 = Error CRC sum
        /// 5 = Error data echo
        /// 6 = Error MRS code / address echo
        /// High byte:
        /// 0 = OK
        /// 1 = Error reading init-parameter MRS code 0xA1
        /// 2 = Error reading bits per position
        /// 3 = Error reading encoder type
        /// 4 = Error reading low-word signal period
        /// 5 = Error reading init-parameter MRS code 0xA3
        /// 6 = Error reading high-word signal period
        /// 7 = Error reading max.distinguishable revolutions
        /// 8 = Error reading init-parameter MRS code 0xA5
        /// 9 = Error reading supported alarms
        /// 10 = Error reading supported warnings
        /// 11 = Error reading nominal increment
        /// distance-coded REF
        /// 12 = Error reading low-word measuring step
        /// 13 = Error reading high-word measuring step
        /// 14 = Error reading measuring direction</param>
        /// <param name="pType">Pointer to a variable in which the encoder type is saved.</param>
        /// <param name="pPeriod">Pointer to a variable in which the signal period of the incremental signals or the number of lines per revolution is saved.</param>
        /// <param name="pStep">Pointer to a variable in which the measuring step of the EnDat position value or the number of measuring steps per revolution is saved.</param>
        /// <param name="pTurns">Pointer to a variable in which the number of resolvable revolutions is saved.</param>
        /// <param name="pRefDist">Pointer to a variable in which the nominal increment of distance-coded REF marks is saved</param>
        /// <param name="pCntDir">Pointer to a variable in which the measuring direction is saved.</param>
        /// <returns></returns>
        [DllImport("ik220dll.dll", EntryPoint = "IK220ConfigEn", CallingConvention = CallingConvention.StdCall)]
        public static extern bool IK220ConfigEn(ushort ax, out ushort pStatus, out ushort pType, out uint pPeriod, out uint pStep, out ushort pTurns, out ushort pRefDist, out ushort pCntDir);

        /// <summary>
        /// 声明读取EnDat编码器绝对脉冲值函数IK220ReadEn
        /// </summary>
        /// <param name="ax"></param>
        /// <param name="pStatus">Pointer to a variable in which the EnDat status is saved.
        /// 0 = OK
        /// 1 = Encoder does not answer or no encoder connected
        /// 2 = Error CRC sum
        /// 3 = Error type A</param>
        /// <param name="pData"></param>
        /// <param name="pAlarm">Pointer to a variable in which the alarm bit is saved 0 = OK 1 = Alarm</param>
        /// <returns></returns>
        [DllImport("ik220dll.dll", EntryPoint = "IK220ReadEn", CallingConvention = CallingConvention.StdCall)]
        public static extern bool IK220ReadEn(ushort ax, out short pStatus, out double pData, out ushort pAlarm);

        //声明设置IK220参数函数IK220WritePar
        [DllImport("ik220dll.dll", EntryPoint = "IK220WritePar", CallingConvention = CallingConvention.StdCall)]
        public static extern bool IK220WritePar(ushort ax, ushort ParNum, int ParVal);

        //读IK220参数函数
        [DllImport("ik220dll.dll", EntryPoint = "IK220ReadPar", CallingConvention = CallingConvention.StdCall)]
        public static extern bool IK220ReadPar(ushort ax, ushort ParNum, out int ParVal);

        //Ascertains whether a REF function is running (Reset, Start or Stop with REF or REF traverse)
        [DllImport("ik220dll.dll", EntryPoint = "IK220RefActive", CallingConvention = CallingConvention.StdCall)]
        public static extern bool IK220RefActive(ushort ax, out byte status);

        //Start reference point traverse，设置后IK220Status寄存器的bit6会置位
        [DllImport("ik220dll.dll", EntryPoint = "IK220DoRef", CallingConvention = CallingConvention.StdCall)]
        public static extern bool IK220DoRef(ushort ax);

        //Interrupt reference pint 
        [DllImport("ik220dll.dll", EntryPoint = "IK220CancelRef", CallingConvention = CallingConvention.StdCall)]
        public static extern bool IK220CancelRef(ushort ax);

        //Defines the type of evaluation of the refrence mark signal 
        [DllImport("ik220dll.dll", EntryPoint = "IK220RefEval", CallingConvention = CallingConvention.StdCall)]
        public static extern bool IK220RefEval(ushort ax, ushort mode);

        //Delete counter with next reference mark
        [DllImport("ik220dll.dll", EntryPoint = "IK220ResetRef", CallingConvention = CallingConvention.StdCall)]
        public static extern bool IK220ResetRef(ushort ax);

        /// <summary>
        /// Reports the status of IK220
        /// </summary>
        /// <param name="ax"></param>
        /// <param name="status">
        /// bit0 1:Counter value latched in register0
        /// bit1 1:Counter value latched in register1
        /// bit2 1:Counter value latched in register2
        /// bit3 1:EnDat call with continous clock
        /// bit4 1:Contamination warning/error
        /// bit5 1:Counter started
        /// bit6 1:REF function active (start, stop, reset, or lathch)
        /// </param>
        /// <returns></returns>
        [DllImport("ik220dll.dll", EntryPoint = "IK220Status", CallingConvention = CallingConvention.StdCall)]
        public static extern bool IK220Status(ushort ax, out uint status);

        //Waits for an active edge of the reference mark
        [DllImport("ik220dll.dll", EntryPoint = "IK220PositionRef", CallingConvention = CallingConvention.StdCall)]
        public static extern bool IK220PositionRef(ushort ax, out double data, out int period, out ushort intpol);
    }
}
